<?php

namespace app\models\user;

use Yii;

/**
 * This is the model class for table "user_commission".
 *
 * @property int $id
 * @property int $user_id 获取得者
 * @property int $fans_id 消费者(可能是自己, 如充值)
 * @property float $balance 余额(佣金),可为负
 * @property float $score 积分, 可为负
 * @property float $ticket 购物券, 可为负
 * @property float $platform_ticket 平台券, 可为负
 * @property int $order_id 订单ID
 * @property int $refill_id 充值ID
 * @property int $merchant_order_id 商家订单ID
 * @property int $group_order_id 拼团订单ID
 * @property int $sign_in_id 签到ID
 * @property int $status 0 待返, 1 已返
 * @property int $award_type 奖类型, 如 1 直推奖
 * @property string $award_name 奖名
 * @property string $create_time
 * @property string $update_time
 * @property int $month_return 是否为月返奖
 */
class UserCommission extends \yii\db\ActiveRecord
{

    public static $AWARD_Type = [
        'SIGN_IN' => 1,

        'MERCHANT_PAY' => 2,

        'BAO_AREA' => 10,

        // up level
        'UP_LEVEL' => 91,

        // brand area
        'RECOMMEND' => 21,
        'EARNINGS' => 22,
        // 'CITY' => 23,
        'TEAM' => 24,
        'EQUAL' => 25,

        'SALE_V' => 26,
        'SALE_W' => 27,

        // group
        'NOT_WIN_BACK' => 30,
        'NOT_WIN' => 31,
        'GROUP_FINISH' => 32,
        'GROUP_V' => 33,
    ];

    public static $AWARD_NAME = [
        'SIGN_IN' => '签到',

        'MERCHANT_PAY' => '线下消费',

        'BAO_AREA' => '爆品区',

        // up level
        'UP_LEVEL' => '升级奖',

        // brand area
        'RECOMMEND' => '直推奖',
        'EARNINGS' => '收益',
        // 'CITY' => '区域奖',
        'TEAM' => '团队奖',
        'EQUAL' => '平级奖',

        'SALE_V' => '销售奖',
        'SALE_W' => '业绩奖',

        // group
        'NOT_WIN_BACK' => '退回本金',
        'NOT_WIN' => '未拼中',
        'GROUP_FINISH' => '成团奖',
        'GROUP_V' => '创业奖',

    ];

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user_commission';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'fans_id', 'order_id', 'refill_id', 'merchant_order_id', 'group_order_id', 'sign_in_id', 'status', 'award_type'], 'integer'],
            [['balance', 'score', 'ticket', 'platform_ticket'], 'number'],
            [['create_time', 'update_time'], 'safe'],
            [['award_name'], 'string', 'max' => 255],

            // month
            [['month_return'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'fans_id' => 'Fans ID',
            'balance' => 'Balance',
            'score' => 'Score',
            'ticket' => 'Ticket',
            'platform_ticket' => 'Platform Ticket',
            'order_id' => 'Order ID',
            'refill_id' => 'Refill ID',
            'merchant_order_id' => 'Merchant Order ID',
            'group_order_id' => 'Group Order ID',
            'sign_in_id' => 'sign_in_id',
            'status' => 'Status',
            'award_type' => 'Award Type',
            'award_name' => 'Award Name',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id']);
    }
}
